<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>

  <body>
    <script>
      // 先同步任务 在异步任务--微任务(先全部执行完) 宏任务(一个执行完) 再看微任务(有的话,全部执行完)
      const promise = new Promise((resolve, reject) => {
        console.log(1);//同步任务
        resolve();
        console.log(2);//同步任务
      });
      promise.then(function(){ 
        console.log(3);// //.then 微任务
      });
      console.log(4);//同步

      setTimeout(function () {
        console.log('5');// 宏任务
        promise.then(function(){ console.log(10);});
      });

      var p = new Promise(function (resolve, reject) {
        console.log('6');// 同步
        for (var i = 0; i < 1000; i++) {
          i == 99 && resolve();
        }
        console.log('7');//同步
      });
      p.then(function () {
        console.log('8');// //.then 微任务
      });
      console.log('9');// 同步
      //1 2 4 6 7 9 3 8    5 10
    </script>
  </body>
</html>
